package com.yunnuo.system.onlineuser.entity;


import org.apache.shiro.session.mgt.SimpleSession;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.Date;

/**
 * 在线回话表
 * Created by 魏洪福(15064000912@163.com) on  2018/6/3.
 */
@Entity
@Table(name="sys_user_online")
public class UserOnline   extends SimpleSession {


    private static final long serialVersionUID = 1L;

    //   用户会话id
    @Id
    private String sessionId;

    //  部门名称
    private String deptName;

    //  登录名称
    private String loginName;

    //  登录IP地址
    private String ipaddr;

    //  浏览器类型
    private String browser;

    //  操作系统
    private String os;

    //  session创建时间
    private Date startTimestamp;

    //  session最后访问时间
    private Date lastAccessTime;

    //  超时时间，单位为分钟
    private Long expireTime;

    // 在线状态
    private OnlineSession.OnlineStatus status = OnlineSession.OnlineStatus.on_line;

    //  备份的当前用户会话
    @Transient
    private OnlineSession session;

    /**
     * 设置session对象
     */
    public static final UserOnline fromOnlineSession(OnlineSession session)
    {
        UserOnline online = new UserOnline();
        online.setSessionId(String.valueOf(session.getId()));
        online.setDeptName(session.getDeptName());
        online.setLoginName(session.getLoginName());
        online.setStartTimestamp(session.getStartTimestamp());
        online.setLastAccessTime(session.getLastAccessTime());
        online.setExpireTime(session.getTimeout());
        online.setIpaddr(session.getHost());
        online.setBrowser(session.getBrowser());
        online.setOs(session.getOs());
        online.setStatus(session.getStatus());
        online.setSession(session);
        return online;
    }



    public String getSessionId()
    {
        return sessionId;
    }

    public void setSessionId(String sessionId)
    {
        this.sessionId = sessionId;
    }

    public String getDeptName()
    {
        return deptName;
    }

    public void setDeptName(String deptName)
    {
        this.deptName = deptName;
    }

    public String getLoginName()
    {
        return loginName;
    }

    public void setLoginName(String loginName)
    {
        this.loginName = loginName;
    }

    public String getIpaddr()
    {
        return ipaddr;
    }

    public void setIpaddr(String ipaddr)
    {
        this.ipaddr = ipaddr;
    }

    public String getBrowser()
    {
        return browser;
    }

    public void setBrowser(String browser)
    {
        this.browser = browser;
    }

    public String getOs()
    {
        return os;
    }

    public void setOs(String os)
    {
        this.os = os;
    }

    @Override
    public Date getStartTimestamp()
    {
        return startTimestamp;
    }
    @Override
    public void setStartTimestamp(Date startTimestamp)
    {
        this.startTimestamp = startTimestamp;
    }
    @Override
    public Date getLastAccessTime()
    {
        return lastAccessTime;
    }
    @Override
    public void setLastAccessTime(Date lastAccessTime)
    {
        this.lastAccessTime = lastAccessTime;
    }

    public Long getExpireTime()
    {
        return expireTime;
    }

    public void setExpireTime(Long expireTime)
    {
        this.expireTime = expireTime;
    }


    public OnlineSession.OnlineStatus getStatus() {
        return status;
    }

    public void setStatus(OnlineSession.OnlineStatus status) {
        this.status = status;
    }
    @Transient
    public OnlineSession getSession()
    {
        return session;
    }

    public void setSession(OnlineSession session)
    {
        this.session = session;
    }


    @Override
    public String toString() {
        return "UserOnline{" +
                ", sessionId='" + sessionId + '\'' +
                ", deptName='" + deptName + '\'' +
                ", loginName='" + loginName + '\'' +
                ", ipaddr='" + ipaddr + '\'' +
                ", browser='" + browser + '\'' +
                ", os='" + os + '\'' +
                ", startTimestamp=" + startTimestamp +
                ", lastAccessTime=" + lastAccessTime +
                ", expireTime=" + expireTime +
                ", status=" + status +
                ", session=" + session +
                '}';
    }
}
